package com.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.persistence.Player;
import com.persistence.Reserve;
import com.persistence.TFootballTeam;
import com.persistence.TPhoto;
import com.persistence.TPlayer;
import com.service.PlayerService;
import com.service.TeamService;

@Controller
@SessionAttributes
public class PlayerController extends AbstractController {
	ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
	TeamService teamDao = (TeamService) context.getBean("teamService");
	PlayerService playerDao = (PlayerService) context.getBean("playerService");
	
	@Override
	@RequestMapping(value = "/savePlayer", method = RequestMethod.POST)
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		String fname = req.getParameter("firstName");
		String lname = req.getParameter("lastName");
		String teamId = req.getParameter("teamId");
		String reserve = req.getParameter("reserve");
		String image = req.getParameter("image");
		String salary = req.getParameter("salary");
		
		TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
		
		
		
		if(reserve.equalsIgnoreCase("0")){
			TPhoto photo = new TPhoto();
			photo.setData(image.getBytes());
			photo.setIdPhoto(playerDao.countAll()+1);
			
			Player player = new Player();
			player.setFirstname(fname);
			player.setLastname(lname);
			player.setIdPlayer(playerDao.countAll()+1);
			player.setJoinDate(new Date());
			player.setSalary(Double.parseDouble(salary));
			player.setTFootballTeam(team);
			player.setTPhoto(photo);
			photo.setTPlayer(player);
			
			playerDao.insert(player);
		}else{
			TPhoto photo = new TPhoto();
			photo.setData(image.getBytes());
			photo.setIdPhoto(playerDao.countAll()+1);
			
			Reserve res1 = new Reserve();
			res1.setFirstname(fname);
			res1.setLastname(lname);
			res1.setCommision(Double.parseDouble(salary));
			res1.setIdPlayer(playerDao.countAll()+1);
			res1.setJoinDate(new Date());
			res1.setTFootballTeam(team);
			res1.setTPhoto(photo);
			photo.setTPlayer(res1);
			
			playerDao.insert(res1);
		}
		
		
		List<TFootballTeam> list2 = teamDao.findAll();
		Map<String, Object> list3 = new HashMap<String, Object>();
		team = teamDao.findById(Integer.parseInt(teamId));
		List<TPlayer> playerList = playerDao.findByProperty("TFootballTeam", team, 0);
		
		Iterator i = playerList.iterator();
		List<Player> players = new ArrayList<Player>();
		List<Reserve> reserves = new ArrayList<Reserve>();
		
		while(i.hasNext()) {
			TPlayer tp = (TPlayer) i.next();
			if(tp instanceof Player){
				players.add((Player)tp);
			}else if(tp instanceof Reserve){
				reserves.add((Reserve)tp);
			}
		}
		
		list3.put("team", list2);
		list3.put("player", players);
		list3.put("reserve", reserves);
		list3.put("team2", team);
		
		res.sendRedirect("main.htm?module=team&action=view&teamid=" + teamId);	
		
		return new ModelAndView("contact", "list3", list3);

	}

}
